﻿using UnityEditor;
using UnityEditor.UI;
using UnityEngine;

[CustomEditor(typeof(ImagePro))]
public class ImageProEditor : ImageEditor
{
    ImagePro img;

    protected override void OnEnable()
    {
        base.OnEnable();
        img = (ImagePro)target;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        img.cornerRadius = EditorGUILayout.FloatField("Corner Radius", img.cornerRadius);
        if (img.cornerRadius < 0) img.cornerRadius = 0;
        float halfSizeX = img.rectTransform.rect.width * 0.5f;
        float halfSizeY = img.rectTransform.rect.height * 0.5f;
        float halfSizeMin = halfSizeX > halfSizeY ? halfSizeY : halfSizeX;
        if (img.cornerRadius > halfSizeMin) img.cornerRadius = halfSizeMin;

        if (img.cornerRadius > 0)
        {
            img.cornerDetail = EditorGUILayout.IntField("Corner Detail", img.cornerDetail);
            if (img.cornerDetail < 1) img.cornerDetail = 1;
            if (img.cornerDetail > 25) img.cornerDetail = 25;

            if (img.texScale < 0) img.texScale = 0;

            img.texScale = EditorGUILayout.FloatField("Tex Scale", img.texScale);
        }

        img.gradient = EditorGUILayout.Toggle("Gradient", img.gradient);
        if (img.gradient)
        {
            img.beginColor = EditorGUILayout.ColorField(img.beginColor);
            img.endColor = EditorGUILayout.ColorField(img.endColor);
            EditorGUILayout.Space();
        }

        img.effect = (ImageProEffect)EditorGUILayout.EnumPopup("Effect", img.effect);
        if (img.effect == ImageProEffect.FlowLight)
        {
            img.flowLightTexture = (Texture)EditorGUILayout.ObjectField("    Light Texture", img.flowLightTexture, typeof(Texture), false);
            img.flowLightLightScale = EditorGUILayout.FloatField("    Light Scale", img.flowLightLightScale);
            img.flowLightIntensity = EditorGUILayout.FloatField("    Intensity", img.flowLightIntensity);
            img.flowLightSpeed = EditorGUILayout.FloatField("    Speed", img.flowLightSpeed);
            img.flowLightAngle = EditorGUILayout.FloatField("    Angle", img.flowLightAngle);
        }

        if (GUI.changed)
        {
            serializedObject.ApplyModifiedProperties();
            EditorUtility.SetDirty(target);
            img.SetAllDirty();
        }
    }
}